//
//  HZGoodsFindCategoriesBar.m
//  app
//
//  Created by 刘华舟 on 15/5/21.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZGoodsFindCategoriesBar.h"

#import "HZFindCategoryModel.h"

static const NSInteger kCategoriesButtonHeight = 100;

@interface HZGoodsFindCategoriesBar()


@property (strong, nonatomic) NSMutableArray* categoryButtons;
@property (strong, nonatomic) NSArray* categoryDatas;

@property (weak, nonatomic) HZGoodsCategoryButton* lastButton;

@end

@implementation HZGoodsFindCategoriesBar

- (NSMutableArray *)categoryButtons{
    if (_categoryButtons == nil) {
        _categoryButtons = [NSMutableArray array];
    }
    return _categoryButtons;
}

-(id)initWithFrame:(CGRect)frame
{
    frame = CGRectOf(frame.origin, [UBO_SCREEN uw:kGoodsFindCategoriesBarWidth oh:frame.size.height]);
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = HZ_MALL_S_LIGHT_GRAY_BACKGROUND_COLOR;
        
        //scrollView
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],
                                                                       [self.ubo uw:kGoodsFindCategoriesBarWidth fh:UB_HEIGHT])];
        self.scrollView.backgroundColor = HZ_CLEAR_COLOR;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.scrollView];
    }
    return self;
}


-(void)showCategories:(NSArray*)categories;
{
    
    self.lastButton = nil;
    
    for(HZGoodsCategoryButton* cell in self.categoryButtons){
        [cell removeFromSuperview];
    }
    
    [self.categoryButtons removeAllObjects];
    self.categoryDatas = categories;
    if ([categories isEmpty]) {
        return;
    }
    
    NSInteger index = 0;
    
    UIImage *leftOrange = [[UIImage imageNamed:kImageButtonLeftLineOrange] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 4, 0, 0) resizingMode:UIImageResizingModeStretch];
    UIImage *leftNone = [[UIImage imageNamed:kImageButtonLeftLineNone] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 4, 4, 4) resizingMode:UIImageResizingModeStretch];
    
    for(HZFindCategoryModel *model in categories){
        HZGoodsCategoryButton *btn = nil;
        
        if (index == 0) {
            btn = [[HZGoodsCategoryButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP], [self.ubo fw:UB_WIDTH uh:kCategoriesButtonHeight])];
        }else{
            HZGoodsCategoryButton *lastBtn = [self.categoryButtons lastObject];
            btn = [[HZGoodsCategoryButton alloc] initWithFrame:CGRectOf([lastBtn.ufo fx:UB_LEFT fy:UB_BOTTOM], [lastBtn.ubo fw:UB_WIDTH fh:UB_HEIGHT])];
        }
        
        [btn setBackgroundColor:HZ_CLEAR_COLOR];
        
        btn.titleLabel.font = kHZRFont_Content_L(1.05f, 1.1f);
        btn.titleLabel.numberOfLines = 2;
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 4, 0, 4);
        
        [btn setTitle:model.name forState:UIControlStateNormal];
        [btn setTitle:model.name forState:UIControlStateSelected];
        [btn setTitleColor:HZ_MALL_TEXT_BLACK_COLOR forState:UIControlStateNormal];
        [btn setTitleColor:HZ_MALL_MAIN_THEME_ORANGE forState:UIControlStateSelected];
        [btn setBackgroundImage:leftNone forState:UIControlStateNormal];
        [btn setBackgroundImage:leftOrange forState:UIControlStateSelected];
        
        btn.selected = NO;
        
        [btn addTarget:self action:@selector(labelDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.scrollView addSubview:btn];
        [self.categoryButtons addObject:btn];
        
        btn.tag = index;
        
        index++;

    }
    
    float scrollContentHeight = [self.ubo uh:kCategoriesButtonHeight] * index;
    
    if (scrollContentHeight < [self.ubo fh:UB_HEIGHT]) {
        scrollContentHeight = [self.ubo fh:UB_HEIGHT];
    }
    
    self.scrollView.contentSize = [self.scrollView.ubo fw:UB_WIDTH oh:scrollContentHeight];
    
    
    self.lastButton = [self.categoryButtons firstObject];
    self.lastButton.selected = YES;
    [self.lastButton setBackgroundColor:HZ_WHITE_COLOR];
    [self.tabSelectedCaller call:[self.categoryDatas firstObject]];
}


-(void)labelDidClicked:(HZGoodsCategoryButton*)btn
{
    if (self.lastButton) {
        [self.lastButton setSelected:NO];
        [self.lastButton setBackgroundColor:HZ_CLEAR_COLOR];
    }
    [btn setSelected:YES];
    [btn setBackgroundColor:HZ_WHITE_COLOR];
    self.lastButton = btn;
    
    CGFloat btnY = btn.ufo.y;
    CGFloat btnMaxY = self.scrollView.contentSize.height - self.scrollView.frame.size.height;
    
    if (btnY <= btnMaxY) {
        [self.scrollView setContentOffset:CGPointMake(0.0f, btnY) animated:YES];
    }else{
        [self.scrollView setContentOffset:CGPointMake(0.0f, btnMaxY) animated:YES];
    }
    
    if (self.tabSelectedCaller) {
        [self.tabSelectedCaller call:[self.categoryDatas objectAtIndex:self.lastButton.tag]];
    }
}

@end
